package com.hlj.ncp.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hlj.ncp.warehouse.entity.ColdStorageThreshold;
import com.hlj.ncp.warehouse.mapper.ColdStorageThresholdMapper;
import com.hlj.ncp.warehouse.service.IColdStorageThresholdService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 冷库温湿度阈值设置 服务层实现
 */
@Service
public class ColdStorageThresholdServiceImpl extends ServiceImpl<ColdStorageThresholdMapper, ColdStorageThreshold> implements IColdStorageThresholdService {

    /**
     * 查询冷库温湿度阈值设置列表
     * 
     * @param coldStorageThreshold 冷库温湿度阈值设置信息
     * @return 冷库温湿度阈值设置
     */
    @Override
    public List<ColdStorageThreshold> selectColdStorageThresholdList(ColdStorageThreshold coldStorageThreshold) {
        LambdaQueryWrapper<ColdStorageThreshold> queryWrapper = new LambdaQueryWrapper<>();
        
        // 构建查询条件
        if (coldStorageThreshold != null) {
            // 按仓库ID查询
            if (coldStorageThreshold.getWarehouseId() != null) {
                queryWrapper.eq(ColdStorageThreshold::getWarehouseId, coldStorageThreshold.getWarehouseId());
            }
            
            // 按仓库名称查询
            if (StringUtils.isNotBlank(coldStorageThreshold.getWarehouseName())) {
                queryWrapper.like(ColdStorageThreshold::getWarehouseName, coldStorageThreshold.getWarehouseName());
            }
            
            // 按冷库区域查询
            if (StringUtils.isNotBlank(coldStorageThreshold.getStorageArea())) {
                queryWrapper.eq(ColdStorageThreshold::getStorageArea, coldStorageThreshold.getStorageArea());
            }
            
            // 按告警级别查询
            if (StringUtils.isNotBlank(coldStorageThreshold.getAlarmLevel())) {
                queryWrapper.eq(ColdStorageThreshold::getAlarmLevel, coldStorageThreshold.getAlarmLevel());
            }
        }
        
        return this.list(queryWrapper);
    }

    /**
     * 查询冷库温湿度阈值设置详细信息
     * 
     * @param thresholdId 阈值设置ID
     * @return 冷库温湿度阈值设置信息
     */
    @Override
    public ColdStorageThreshold selectColdStorageThresholdById(Long thresholdId) {
        return this.getById(thresholdId);
    }
    
    /**
     * 根据仓库ID和区域查询阈值设置
     * 
     * @param warehouseId 仓库ID
     * @param storageArea 冷库区域
     * @return 阈值设置
     */
    @Override
    public ColdStorageThreshold selectThresholdByWarehouseAndArea(Long warehouseId, String storageArea) {
        return baseMapper.selectThresholdByWarehouseAndArea(warehouseId, storageArea);
    }

    /**
     * 新增冷库温湿度阈值设置
     * 
     * @param coldStorageThreshold 冷库温湿度阈值设置信息
     * @return 结果
     */
    @Override
    public int insertColdStorageThreshold(ColdStorageThreshold coldStorageThreshold) {
        // 设置创建时间
        coldStorageThreshold.setCreateTime(LocalDateTime.now());
        
        return this.save(coldStorageThreshold) ? 1 : 0;
    }

    /**
     * 修改冷库温湿度阈值设置
     * 
     * @param coldStorageThreshold 冷库温湿度阈值设置信息
     * @return 结果
     */
    @Override
    public int updateColdStorageThreshold(ColdStorageThreshold coldStorageThreshold) {
        // 设置更新时间
        coldStorageThreshold.setUpdateTime(LocalDateTime.now());
        
        return this.updateById(coldStorageThreshold) ? 1 : 0;
    }

    /**
     * 批量删除冷库温湿度阈值设置
     * 
     * @param thresholdIds 需要删除的阈值设置ID
     * @return 结果
     */
    @Override
    public int deleteColdStorageThresholdByIds(Long[] thresholdIds) {
        return this.removeByIds(Arrays.asList(thresholdIds)) ? thresholdIds.length : 0;
    }

    /**
     * 删除冷库温湿度阈值设置信息
     * 
     * @param thresholdId 阈值设置ID
     * @return 结果
     */
    @Override
    public int deleteColdStorageThresholdById(Long thresholdId) {
        return this.removeById(thresholdId) ? 1 : 0;
    }
} 